package com.henme.crm.web.admin;

import com.google.common.collect.Lists;
import com.henme.crm.entity.admin.Authority;
import com.henme.crm.entity.admin.Role;
import com.henme.crm.service.admin.AuthorityService;
import com.henme.crm.service.admin.RoleService;
import com.henme.crm.util.dwz.AjaxDone;
import com.henme.crm.util.page.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/role")
public class RoleController    {
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private AuthorityService authorityService;
	
	/**
	 * page 获得从页面传过来的分页信息
	 * @param page
	 * @param model
	 * @return
	 */
	@RequiresPermissions("role:view")
	@RequestMapping("/list")
	public String list(@ModelAttribute("page") Page<Role> page,
			@ModelAttribute("role") Role role,
			@RequestParam(value="navtabid",required = false) String navtabid,
			Model model) {

		page.setT(role);
		page=roleService.getRoleList(page);
		model.addAttribute("page", page);
		model.addAttribute("role", role);
		model.addAttribute("navtabid", navtabid);
		return "role/list";
	}




    @RequestMapping("/lookup")
    public String lookup(@ModelAttribute("page") Page<Role> page,
                       @ModelAttribute("role") Role role,
                       @RequestParam(value="navtabid",required = false) String navtabid,
                       Model model) {
        page.setT(role);
        page=roleService.getRoleList(page);
        model.addAttribute("page", page);
        model.addAttribute("role", role);
        model.addAttribute("navtabid", navtabid);
        return "role/lookup";
    }
	/**
	 * 编辑页面
	 * @param model
	 * @param id
	 * @param navtabid
	 * @return
	 */
	@RequiresPermissions("role:edit")
	@RequestMapping(value="/edit/{id}")
	public String prepare(Model model,@PathVariable("id") Long id,
			@RequestParam("navtabid") String navtabid
		) {
		model.addAttribute("navtabid", navtabid);
		model.addAttribute("role", roleService.getRole(id));
		return "role/edit";
	}
	
	/**
	 * 保存更新
	 * @param role
	 * @param navtabid
	 * @return
	 */
	@RequiresPermissions("role:edit")
	@ResponseBody
	@RequestMapping("/update")
	public AjaxDone update(@ModelAttribute("role")Role role,
			@RequestParam("navtabid") String navtabid
			) 
	{
		roleService.updateRole(role);
		AjaxDone a=new AjaxDone();
		a.setStatusCode("200");
		a.setCallbackType("closeCurrent");
		a.setMessage("更新成功");
		a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequiresPermissions("role:delete")
	@ResponseBody
	@RequestMapping("/delete/{id}")
	public AjaxDone delete(@PathVariable("id") Long id) {
		roleService.deleteRole(id);
		AjaxDone a=new AjaxDone();
		a.setStatusCode("200");
		a.setMessage("删除成功");
		return a;
	}
	
	/**
	 * 新增页面
	 * @param model
	 * @param navtabid
	 * @return
	 */
	@RequiresPermissions("role:add")
	@RequestMapping("/add")
	public String add(Model model,@RequestParam("navtabid") String navtabid) {
		model.addAttribute("navtabid", navtabid);
		return "role/edit";
	}

	/**
	 * 保存新增
	 * @param role
	 * @param navtabid
	 * @return
	 */
	@RequiresPermissions("role:add")
	@ResponseBody
	@RequestMapping("/insert")
	public AjaxDone insert(@ModelAttribute("role")Role role,
			@RequestParam("navtabid") String navtabid) {
		roleService.insertRole(role);
		AjaxDone a=new AjaxDone();
		a.setStatusCode("200");
		a.setCallbackType("closeCurrent");
		a.setMessage("新增成功");
		a.setForwardUrl(""); 
		a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}

    @RequiresPermissions("role:permission")
    @RequestMapping("/permissiontree/{roleId}")
    public String permissiontree(@PathVariable("roleId") Long roleId,
                             Model model) {
        List<Authority> list=authorityService.getAllAuthorityList();
        List<Authority> list1=authorityService.getAuthorityListByRoleId(roleId);
        List<Authority> result=Lists.newArrayList();
        Role role=roleService.getRole(roleId);

        for(Authority r:list){
            for(Authority r1:list1){
                if(r.getId().equals(r1.getId())){
                    r.setChecked(true);
                    continue;
                }
            }
            result.add(r);
        }
        Map<String,List<Authority>> map=new HashMap<String, List<Authority>>();
        Set<String> key=new HashSet<String>() ;
        for(Authority a:result){
                key.add(a.getModulename());
        }
        for(String k:key){
            List<Authority> l=Lists.newArrayList();
               for(Authority a:result){
                   if(k.equals(a.getModulename())){
                       l.add(a);
                   }
               }
            map.put(k,l);
        }
        model.addAttribute("list",result);
        model.addAttribute("role", role);
        model.addAttribute("map",map);
        return "role/permissiontree";
    }




    @RequiresPermissions("role:permission")
    @ResponseBody
    @RequestMapping("/savepermissiontree/{roleId}")
    public AjaxDone savepermissiontree(@PathVariable("roleId") Long roleId,
                                   @RequestParam("navtabid") String navtabid,HttpServletRequest request
    )
    {
        String permissionid= request.getParameter("permissionlist");
        String [] permissionids    =  permissionid.split(",")   ;
        roleService.savePermission(roleId,permissionids);
        AjaxDone a=new AjaxDone();
        a.setStatusCode("200");
        a.setCallbackType("closeCurrent");
        a.setMessage("更新成功");
        a.setForwardUrl("");
        return a;
    }
}

